import { useSettingsStore } from "../stores/settings"


export function generateImage(params: OpenAi.Text2ImageRequest): Promise<OpenAi.Text2ImageResponse[]> {
    const settingsStore = useSettingsStore()
    if (settingsStore.apiKey == '') {
        return Promise.reject(new Error('api key is null'))
    }
    const baseUrl = settingsStore.proxy || 'https://api.openai.com'
    return fetch(`${baseUrl}/v1/images/generations`, {
        method: 'POST',
        headers: {
            'Authorization': `Bearer ${settingsStore.apiKey}`,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(params)
    }).then(resp => resp.json()).then(res => {
        return res.data as OpenAi.Text2ImageResponse[]
    })
}

